package xyz.perkd.form;

import xyz.perkd.process.IProcessor;
import xyz.perkd.process.impl.Processor;
import xyz.perkd.process.impl.Processor2;
import xyz.perkd.util.DateTimeUtil;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.File;



public class MainForm {
    public JPanel main;
    private JTextField filePath;
    private JButton fileSelector;
    private JLabel fileLabel;
    private JButton start;
    private Box loggerBox;
    private JScrollPane loggerWrap;

    public MainForm () {
        fileSelector.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "通讯录文件(*.pdf)", "pdf");
            fileChooser.setFileFilter(filter);
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            fileChooser.showOpenDialog(null);
            filePath.setText(fileChooser.getSelectedFile().getPath());
        });

        start.addActionListener(e -> {
            String path = filePath.getText();
            if (path == null || path.isEmpty()) {
                JOptionPane.showMessageDialog(main, "请先选择要处理的文件", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            File file = new File(path);
            if (!file.exists()) {
                JOptionPane.showMessageDialog(main, "文件不存在，请确认后重新选择", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            IProcessor processor = new Processor2();
            processor.process(path, listener);

            loggerBox.removeAll();
        });

        loggerBox = Box.createVerticalBox();
        loggerWrap.setViewportView(loggerBox);
        loggerWrap.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        filePath.setTransferHandler(new FileDropHandler(filePath));
    }

    private final Processor.ProcessListener listener = new Processor.ProcessListener() {
        private JTextArea getJTextArea(String text, int color) {
            JTextArea logger = new JTextArea(text);
            logger.setPreferredSize(new Dimension(1600, 20));
            logger.setMaximumSize(new Dimension(1600, 20));
            if (color == 1) {
                logger.setForeground(Color.BLUE);
            } else if (color == 2) {
                logger.setForeground(Color.RED);
            }
            return logger;
        }
        @Override
        public void onStart() {
            // logger.append(DateTimeUtil.getLoggerStamp() + "开始处理\n\n");
            JTextArea logger = getJTextArea(DateTimeUtil.getLoggerStamp() + "开始处理", 0);
            loggerBox.add(logger);
            loggerBox.layout();
        }

        @Override
        public void onProcess(String message, int color) {
            // logger.append(DateTimeUtil.getLoggerStamp() + message + "\n\n");
            JTextArea logger = getJTextArea(DateTimeUtil.getLoggerStamp() + message, color);
            loggerBox.add(logger);
            loggerBox.layout();
        }

        @Override
        public void onFinish(String outputPath) {
            // logger.append(DateTimeUtil.getLoggerStamp() + "完成处理\n\n");
            JTextArea logger = getJTextArea(DateTimeUtil.getLoggerStamp() + "完成处理", 0);
            loggerBox.add(logger);
            loggerBox.layout();

            JButton button = new JButton("打开目录");
            JButton[] buttons = {button};
            button.addActionListener(e -> {
                try {
                    Desktop.getDesktop().open(new File(outputPath));
                    Window win = SwingUtilities.getWindowAncestor(button);
                    win.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            });
            JOptionPane.showOptionDialog(main,
                    "全部处理完成，生成文件在目录 " + outputPath + " 点击打开",
                    "温馨提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, buttons, buttons[0]);
        }
    };
}
